//
//  HRSearchFirendsViewController.m
//  Hear
//
//  Created by mac on 2017/12/4.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRSearchFirendsViewController.h"
#import "HRSegmentView.h"
#import "JLDoubleSlider.h"
#import "HRSearchUsersResultViewController.h"
@interface HRSearchFirendsViewController ()
@property (nonatomic, strong)HRSegmentView *segmentView;
@property(nonatomic, strong)UIView *conditionView;
@property(nonatomic, strong)UIButton *stayTempButton;
@property(nonatomic, strong)UIButton *sexTempButton;
@property(nonatomic, strong)UIView *accurateView;
@property (nonatomic, strong)UITextField *searchView;
@property(nonatomic, assign)BOOL isSecondSearch;
@property(nonatomic, copy)NSString *minAgeStr;
@property(nonatomic, copy)NSString *maxAgeStr;

@end

@implementation HRSearchFirendsViewController

-(UITextField *)searchView{
    if (_searchView == nil){
        _searchView = [[UITextField alloc] initWithFrame:CGRectMake(20, _segmentView.bottom+20, kFullWidth-40, 50)];
        _searchView.backgroundColor = [UIColor whiteColor];
        _searchView.delegate = self;
        UIButton *leftImageView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, _searchView.height, _searchView.height)];
        [leftImageView setImage:[UIImage imageNamed:@"搜索-1"] forState:0];;
        _searchView.tintColor = [UIColor blueColor];
        _searchView.leftView = leftImageView;
        _searchView.leftViewMode = UITextFieldViewModeAlways;
        _searchView.placeholder = @"输入ID号/个性签名";
        _searchView.clearButtonMode = UITextFieldViewModeWhileEditing;
        _searchView.font = CustomUIFont(14);
        _searchView.layer.cornerRadius = 5;
        _searchView.clipsToBounds = YES;
        _searchView.returnKeyType = UIReturnKeySearch;
        _searchView.hidden = YES;
    }
    return _searchView;
}

-(HRSegmentView *)segmentView{
    if (!_segmentView){
        WeakSelf
        _segmentView = [[HRSegmentView alloc] initWithFrame:CGRectMake(0, kTopHeight, kFullWidth, 50) andTitleArray:@[@"条件搜",@"精准搜"]];
        _segmentView.backgroundColor = CustomColor(240, 240, 240, 1);
        _segmentView.cutTypeViewBlock = ^(NSUInteger index){
            if (index ==0){
                weakSelf.searchView.hidden = YES;
                weakSelf.conditionView.hidden = NO;
                weakSelf.isSecondSearch = NO;
            }else{
                weakSelf.searchView.hidden = NO;
                weakSelf.conditionView.hidden = YES;
                weakSelf.isSecondSearch = YES;
            }
        };
    }
    return _segmentView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CustomColor(240, 240, 240, 1);
    self.title = @"查找";
    
    [self setupView];
    
    [self.view addSubview:self.searchView];
    

    
    
}

- (void)setupView{
    [self.view addSubview:self.segmentView];
    
    self.conditionView = [[UIView alloc] initWithFrame:CGRectMake(0, self.segmentView.bottom+20, kFullWidth, 150)];
    self.conditionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.conditionView];

    NSArray *firstArr = @[@"留声机",@"无",@"有",@"不限"];
    CGFloat buttonwidth = kFullWidth/firstArr.count;
    CGFloat buttonHeight = self.conditionView.height/3;

    for (int i =0; i <firstArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:0];
        button.frame = CGRectMake(i*buttonwidth, 0, buttonwidth, buttonHeight);
        [button setTitle:firstArr[i] forState:0];
        [button setTitleColor:FirstTextColor forState:0];
        button.titleLabel.font = CustomUIFont(14);
        [self.conditionView addSubview:button];
        button.backgroundColor = [UIColor whiteColor];
        if(i ==3){
            [button setTitleColor:[UIColor whiteColor] forState:0];
            button.backgroundColor = BlueColor;
            self.stayTempButton = button;
        }
        if (i !=0){
            button.tag = i-1;
            [button addTarget:self action:@selector(stayClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    UILabel *firstLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.conditionView.width, 1)];
    firstLine.backgroundColor = CustomColor(200, 200, 200, 1);
    [self.conditionView addSubview:firstLine];
    
    NSArray *secondArr = @[@"性别",@"女",@"男",@"不限"];
    for (int i =0; i <secondArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:0];
        button.frame = CGRectMake(i*buttonwidth, buttonHeight, buttonwidth, buttonHeight);
        [button setTitle:secondArr[i] forState:0];
        [button setTitleColor:FirstTextColor forState:0];
        button.titleLabel.font = CustomUIFont(14);
        [self.conditionView addSubview:button];
        button.backgroundColor = [UIColor whiteColor];
        if(i ==3){
            [button setTitleColor:[UIColor whiteColor] forState:0];
            button.backgroundColor = BlueColor;
            self.sexTempButton = button;
        }
        if (i !=0){
            button.tag = i-1;
            [button addTarget:self action:@selector(sexClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    UILabel *secondLine = [[UILabel alloc] initWithFrame:CGRectMake(0, buttonHeight, self.conditionView.width, 1)];
    secondLine.backgroundColor = CustomColor(200, 200, 200, 1);
    [self.conditionView addSubview:secondLine];
    
    UILabel *thirdLine = [[UILabel alloc] initWithFrame:CGRectMake(0, buttonHeight*2, self.conditionView.width, 1)];
    thirdLine.backgroundColor = CustomColor(200, 200, 200, 1);
    [self.conditionView addSubview:thirdLine];
    
    UIButton *ageButton = [UIButton buttonWithType:0];
    [ageButton setTitle:@"年龄" forState:0];
    [ageButton setTitleColor:FirstTextColor forState:0];
    ageButton.titleLabel.font = CustomUIFont(14);
    [self.conditionView addSubview:ageButton];
    ageButton.frame = CGRectMake(0, buttonHeight*2, buttonwidth, buttonHeight);
    
    
    
    UILabel *minAgeLabel = [UILabel new];
    minAgeLabel.text = @"18";
    minAgeLabel.font = CustomUIFont(14);
    minAgeLabel.textColor = FirstTextColor;
    [self.conditionView  addSubview:minAgeLabel];
    minAgeLabel.frame = CGRectMake(ageButton.right, ageButton.top, 30, ageButton.height);
    
    
    CGFloat sliderWidth = self.conditionView.width-minAgeLabel.right-minAgeLabel.width-20;
    UILabel *maxAgeLabel = [UILabel new];
    maxAgeLabel.text = @"50+";
    maxAgeLabel.font = CustomUIFont(14);
    maxAgeLabel.textColor = FirstTextColor;
    [self.conditionView  addSubview:maxAgeLabel];
    maxAgeLabel.frame = CGRectMake(self.conditionView.width-minAgeLabel.width-10, minAgeLabel.top, minAgeLabel.width, minAgeLabel.height);
    
    self.minAgeStr = @"18";
    self.maxAgeStr = @"50";
    
    JLDoubleSlider *slider = [[JLDoubleSlider alloc]initWithFrame:CGRectMake(minAgeLabel.right, ageButton.top+3, sliderWidth, buttonHeight)];
    slider.minNum = 18;
    slider.maxNum = 50;
    slider.minTintColor = CustomColor(189, 189, 189, 1);
    slider.maxTintColor = CustomColor(189, 189, 189, 1);
    slider.mainTintColor = BlueColor;
    [self.conditionView addSubview:slider];
    slider.changeMinValueBlock = ^(NSString *value) {
        if ([value intValue]<=slider.maxNum){
            minAgeLabel.text = [NSString stringWithFormat:@"%d",[value intValue]];
            self.minAgeStr = minAgeLabel.text;
        }
    };
    slider.changeMaxValueBlock = ^(NSString *value) {
        if ([value intValue]<=slider.maxNum){
            maxAgeLabel.text = [NSString stringWithFormat:@"%d",[value intValue]];
            self.maxAgeStr = maxAgeLabel.text;
        }
    };
    
    UILabel *lastLine = [[UILabel alloc] initWithFrame:CGRectMake(0, buttonHeight*3-1, self.conditionView.width, 1)];
    lastLine.backgroundColor = CustomColor(200, 200, 200, 1);
    [self.conditionView addSubview:lastLine];
    
    
    
    
    UIButton *enterButton = [UIButton buttonWithType:0];
    [enterButton setTitle:@"搜索" forState:0];
    [enterButton setTitleColor:[UIColor whiteColor] forState:0];
    enterButton.titleLabel.font = CustomUIFont(14);
    [enterButton addTarget:self action:@selector(enterButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:enterButton];
    enterButton.frame = CGRectMake(40, self.conditionView.bottom+50, kFullWidth-80, 40);
    enterButton.layer.cornerRadius = 8;
    enterButton.clipsToBounds = YES;
    enterButton.backgroundColor = BlueColor;
}

- (void)enterButtonClick{
    [MobClick event:@"Hearer10"];
    [self.view endEditing:NO];
    HRSearchUsersResultViewController *VC = [HRSearchUsersResultViewController new];
    if (self.isSecondSearch){// 精准搜
        if (!self.searchView.text.length){
            [MBProgressHUD showMessage:@"请输入ID号或者个性签名" finishBlock:nil];
            return;
        }
        VC.Keywords = self.searchView.text;
    }else{// 条件搜
        NSMutableDictionary *paramDict = [NSMutableDictionary dictionary];
        paramDict[@"Sex"] = [NSString stringWithFormat:@"%d",self.sexTempButton.tag];
        paramDict[@"StartAge"] = self.minAgeStr;
        paramDict[@"EndAge"] = self.maxAgeStr;
        paramDict[@"SoundExists"] = [NSString stringWithFormat:@"%d",self.stayTempButton.tag];
        VC.paramDict = paramDict;
    }
    [self.navigationController pushViewController:VC animated:YES];
}




- (void)stayClick:(UIButton *)button{
    self.stayTempButton.backgroundColor = [UIColor whiteColor];
    [self.stayTempButton setTitleColor:FirstTextColor forState:0];
    self.stayTempButton = button;
    [self.stayTempButton setTitleColor:[UIColor whiteColor] forState:0];
    self.stayTempButton.backgroundColor = BlueColor;
}

- (void)sexClick:(UIButton *)button{
    self.sexTempButton.backgroundColor = [UIColor whiteColor];
    [self.sexTempButton setTitleColor:FirstTextColor forState:0];
    self.sexTempButton = button;
    [self.sexTempButton setTitleColor:[UIColor whiteColor] forState:0];
    self.sexTempButton.backgroundColor = BlueColor;
}




-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:NO];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
